// app.js
// https: //github.com/wechat-miniprogram/mobx-miniprogram-bindings
import Hook from 'spa-custom-hooks';
import './lib/lodash-fix';
import _ from './lib/lodash';
import {
  getViewHeight
} from './utils/dom';
import {
  SystemApi
} from './common/apis/system';
import {
  AuthApi
} from './common/apis/login';
import {
  MAIN_PAGES
} from './config/router.config';
import {
  LoginDeptList,
  LoginDeptTeamUser,
  LoginDicList,
  LoginUserInfo,
  LoginUserList
} from './utils/login';
wx.$_ = _;
let globalData = {
  userInfo: null,
  acitvedIndex: 0,
  // 视口高度
  viewHeight: 0,
  // 部门小组人员
  deptTeamUserList: [],
  // 字典
  dicList: [],
  // 部门
  deptList: [],
  // 用户
  userList: [],
  // 版本
  version: {
    dict: '',
    dept: '',
    team: '',
    user: ''
  }
}
const Hooks = {
  'UserInfo': {
    name: 'UserInfo',
    watchKey: 'userInfo',
    deep: true,
    onUpdate(val) {
      //userinfo里的nickName非空则表示命中此钩子
      return !!val?.userId || false;
    }
  },
  'DeptList': {
    name: 'DeptList',
    watchKey: 'deptList',
    deep: true,
    onUpdate(val) {
      //userinfo里的nickName非空则表示命中此钩子
      return !!val?.length
    }
  }
}
Hook.install(Hooks, globalData)
App({
  globalData,
  async initUserInfo() {
    const res = await LoginUserInfo()
    this.globalData.userInfo = res
  },
  async initDeptTeamUser() {
    const res = await LoginDeptTeamUser()
    this.globalData.deptTeamUserList = res
  },
  async initDicList() {
    const res = await LoginDicList()
    this.globalData.dicList = res

  },
  async initDeptList() {
    const res = await LoginDeptList()
    this.globalData.deptList = res

  },

  async initUserList() {
    const res = await LoginUserList()
    this.globalData.userList = res

  },

  // 登录初始化
  loginInit() {
    const self = this
    const token = wx.getStorageSync('token')
    if (!token) {

      wx.login({
        success: (res) => {
          const {
            code
          } = res
          if (code) {
            const data = {
              // 小程序code
              code: code,
              // 头像
              headPortrait: '',
              // 无码报修医院标识
              hospitalId: '',
              // 小程序唯一标识
              openId: '',
              // 用户号码
              phone: '',
              // 开放平台唯一标识
              unionId: '',
            }
            AuthApi.login(data, (rs) => {
              console.log(rs);
              //  未绑定手机号
              if (rs.code == 10) {
                // 跳转至授权页面
                wx.showToast({
                  icon: 'none',
                  title: '未绑定手机号,即将跳转至授权页面',
                  duration: 2000,
                })
                wx.$_.delay(function () {
                  wx.hideToast()
                  wx.redirectTo({
                    url: MAIN_PAGES.AUTH.router,
                  })
                }, 1000)

              } else if (rs.code == 200) {
                wx.setStorageSync('token', rs.data)
                self.globalData.token = rs.data
                self.initUserInfo()
                self.initDeptTeamUser()
                self.initDicList()
                self.initDeptList()
                self.initUserList()
              } else {
                // 
              }
            })
          }
        },

      })
    }


    // wx.getUserProfile({
    //   desc: '用于完善会员资料',
    //   success: (res) => {
    //     console.log(res);


    //   }
    // })
  },
  onLaunch() {
    const self = this
    this.loginInit()

    getViewHeight((height) => {
      self.globalData.viewHeight = height
    })

  },

})